'''
@Author: ysz
@Date: 2019-12-24 10:20:01
@LastEditors  : ysz
@LastEditTime : 2020-01-09 12:19:16
@Description: 数据存入数据库模块
'''

import pymysql
class MysqlHelp(object):
    
    def __init__(self):
        self.conn=pymysql.connect(host='47.95.143.136',user='root',password='',port=3306,db='loupan',charset='utf8')
        self.cursor=self.conn.cursor()
        
    def query_data(self,**args):
        if args['house_type']=='secondary':
            sql=f"select * from db_housesecond_sh where title='{args['title']}'"
            if self.execute_sql(sql,mode='r'):
                print('数据库里有这个标题')
                return False
            sql=f"select * from db_housesecond_sh where area={args['area']} and owner_phone='{args['phone']}' and xq='{args['xq']}'"
            if self.execute_sql(sql,mode='r'):
                print('数据有这个人的电话号码和小区和面积')
                return False
        else:
            sql=f"select * from db_houserent_sh where title='{args['title']}'"
            if self.execute_sql(sql,mode='r'):
                print('数据库里有这个标题')
                return False
            sql=f"select * from db_houserent_sh where phone='{args['phone']}' and xq='{args['xq']}' and  money={args['money']} and configuration='{args['configuration']}' and `fyms`='{args['desc']}' and rent_way='{args['rent_way']}'"
            if self.execute_sql(sql,mode='r'):
                print('数据有这个人的姓名和小区，价钱，配置，描述，租借方式')
                return False 
        return True

    def execute_sql(self,sql,data='',mode='w'):
        if mode=='w':
            self.cursor.execute(sql,data)
            data=self.conn.insert_id()
            self.conn.commit()
        else:
            self.cursor.execute(sql)
            data=self.cursor.fetchone()
        return data
        
    def __del__(self):
        self.cursor.close()
        self.conn.close()

if __name__ == "__main__":
    my=MysqlHelp()
    # sql='insert into db_housesecond_sh(about_id,about_url,title,model1,model2,model3,area,total,unit,direction,layer,total_layer,isdecorate,decorate,`desc`,xq,wz,`status`,create_time,imgs,owner_name,owner_phone,sf,yg,zyqs,`type`,ncq,nj) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'
    sql='select * from db_housesecond_sh where title="天光单间低价出售"'
    print(my.execute_sql(sql,mode='r'))
    

